package nxu.it.controller;

import io.undertow.util.StatusCodes;

import nxu.it.dao.Impl.JdbiTodoServiceImpl;
import nxu.it.dao.TodoService;
import nxu.it.model.Todo;
import nxu.it.security.NeedLoginHandler;
import nxu.it.util.SecurityUtil;
import org.noear.solon.annotation.*;
import org.noear.solon.boot.web.MimeType;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.core.handle.ModelAndView;

import java.util.*;

@Controller
public class TodoController {
    private static TodoService todoService = new JdbiTodoServiceImpl();
    @Mapping(value = "/todo/list",method = MethodType.GET,produces = MimeType.TEXT_HTML_VALUE)
    @Before(NeedLoginHandler.class)
    public ModelAndView list(Context ctx) {

        List<Todo> todoList = SecurityUtil.getCurrentUser(ctx)
                .map(currentUsername -> todoService.listByUser(currentUsername))
                .orElse(new ArrayList<>());
        String username = SecurityUtil.getCurrentUser(ctx).orElse("");
        ModelAndView mv = new ModelAndView("todo/list.ftl");
        mv.put("todoList", todoList);
        mv.put("username", username);
        return mv;
    }
    @Mapping(value = "/todo/save",method = MethodType.POST,produces = MimeType.TEXT_HTML_VALUE)
    @Before(NeedLoginHandler.class)
    public void save(Context ctx,String content) {

        Todo todo = new Todo(content);
        SecurityUtil.getCurrentUser(ctx)
                .ifPresent(username ->{
                    todo.setUsername(username);
                    todoService.save(todo);
                });
        ctx.redirect("/todo/list", StatusCodes.SEE_OTHER);
    }

    @Mapping(value = "/todo/check",method = MethodType.GET,produces = MimeType.TEXT_HTML_VALUE)
    @Before(NeedLoginHandler.class)
    public void check(Context ctx,int id) {
        SecurityUtil.getCurrentUser(ctx).ifPresent(username ->{
            todoService.checkByUser(id,username);
        });
        ctx.redirect("/todo/list", StatusCodes.SEE_OTHER);
    }

    @Mapping(value = "/todo/edit",method = MethodType.GET,produces = MimeType.TEXT_HTML_VALUE)
    @Before(NeedLoginHandler.class)
    public ModelAndView edit(Context ctx,int id) {
        Optional<String> currentUser = SecurityUtil.getCurrentUser(ctx);
        Optional<Todo> todoOpt = todoService.getByUser(id,currentUser.get());

        if(todoOpt.isEmpty()){
            return new ModelAndView("todo/not-found.ftl");
        }
        ModelAndView mv = new ModelAndView("todo/edit.ftl");
        mv.put("todo",todoOpt.get());
        return mv;
    }

    @Mapping(value = "/todo/update",method = MethodType.POST,produces = MimeType.TEXT_HTML_VALUE)
    @Before(NeedLoginHandler.class)
    public void update(Context ctx,Todo todo) {
        Optional<String> currentUser = SecurityUtil.getCurrentUser(ctx);
        todoService.updateByUser(todo,currentUser.get());
        ctx.redirect("/todo/list", StatusCodes.SEE_OTHER);
    }
    @Mapping(value = "/todo/delete",method = MethodType.GET,produces = MimeType.TEXT_HTML_VALUE)
    @Before(NeedLoginHandler.class)
    public void delete(Context ctx,int id) {
        Optional<String> currentUser = SecurityUtil.getCurrentUser(ctx);
        todoService.deleteByUser(id,currentUser.get());
        ctx.redirect("/todo/list", StatusCodes.SEE_OTHER);
    }

}
